//
//  YKVersionManager.m
//  YK_BaseTools
//
//  Created by edward on 2021/10/18.
//  Copyright © 2021 Edward. All rights reserved.
//

#import "YKVersionManager.h"

@implementation YKVersionManager


+ (void)cheakVersion:(NSString *)version callBack:(void(^)(NSComparisonResult result))callBack
{
    if (callBack) {
        NSString *systemVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
        callBack([YKVersionManager contrastVersion:version toVersion:systemVersion useSeparatedString:@"."]);
    }
}

// MARK: - 版本号大小比较
+ (NSComparisonResult)contrastVersion:(NSString *)version1 toVersion:(NSString *)version2 useSeparatedString:(NSString *)separatedString {
    // 为空，以0处理
    if (!version1) {
        version1 = @"0";
    }
    if (!version2) {
        version2 = @"0";
    }
    // 获取版本号字段
    NSArray *version1Arr = [version1 componentsSeparatedByString:separatedString];
    NSArray *version2Arr = [version2 componentsSeparatedByString:separatedString];
    NSInteger v1Count = version1Arr.count;
    NSInteger v2Count = version2Arr.count;
    // 取字段最多的，进行循环比较，
    NSInteger maxCount = (v1Count < v2Count) ? v2Count : v1Count;
    NSComparisonResult result = NSOrderedSame;
    for (NSInteger i = 0; i < maxCount; i ++) {
        NSInteger value1 = (i < v1Count)?[[version1Arr objectAtIndex:i] integerValue]:0;
        NSInteger value2 = (i < v2Count)?[[version2Arr objectAtIndex:i] integerValue]:0;
        
        if (value1 < value2) {
            result = NSOrderedAscending;
            break;
        }
        if (value1 > value2) {
            result = NSOrderedDescending;
            break;
        }
    }
    
    return result;
}

@end
